package com.hooker.penetration

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.hooker.hook.Rule
import com.hooker.hook.RuleHooker
import com.hooker.settings.SettingHelper
import com.sdk.hooker.penetration.R

class AddRuleActivity : AppCompatActivity() {

    lateinit var editName: EditText
    lateinit var editClass: EditText
    lateinit var editMethod: EditText
    lateinit var editParams: EditText
    lateinit var btnAddRule: Button


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_rule)
        initView()
    }

    private fun initView() {
        editName = findViewById(R.id.edit_name)
        editClass = findViewById(R.id.edit_class)
        editMethod = findViewById(R.id.edit_method)
        editParams = findViewById(R.id.edit_params)
        btnAddRule = findViewById(R.id.btn_add_rule)
        btnAddRule.setOnClickListener {
            setRuleData()
        }
    }

    private fun setRuleData() {
        var name = editName.text.toString()
        var className = editClass.text.toString()
        var method = editMethod.text.toString()
        var param = editParams.text.toString()
        if ("" == name) {
            Toast.makeText(this@AddRuleActivity, "名字不能为空", Toast.LENGTH_SHORT).show()
        }
        if ("" == className) {
            Toast.makeText(this@AddRuleActivity, "类名不能为空", Toast.LENGTH_SHORT).show()
        }
        if ("" == method) {
            Toast.makeText(this@AddRuleActivity, "方法名不能为空", Toast.LENGTH_SHORT).show()
        }

        var rule = Rule(className, method)
        rule.name = name
        var params = param.split(",")
        rule.params = params
        RuleHooker.getInstance().listener?.addRule(this, rule)
    }
}